package com.evil.application.strategy.application;

import com.evil.application.enums.order.OrderSpecialControlEnum;
import com.evil.application.pojo.dto.CopyApplicationDTO;
import com.evil.application.pojo.dto.apply.CopyAppApplySettingReqDTO;
import com.evil.application.pojo.dto.form.CopyAppFormReqDTO;
import com.evil.application.pojo.dto.form.control.save.CopyControlDTO;
import com.evil.application.pojo.dto.notify.CopyAppNotifySettingReqDTO;
import com.evil.application.pojo.entity.ApplicationInfo;
import com.evil.application.service.*;
import com.evil.common.application.enums.ApplicationTypeEnum;
import com.evil.common.core.util.StreamUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 应用表单流程操作
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@Service
@AllArgsConstructor
public class FormApplicationOperation implements ApplicationOperation {

    private final ApplicationFormService applicationFormService;

    private final ApplicationApplySettingService applicationApplySettingService;

    private final ApplicationNotifySettingService applicationNotifySettingService;

    private final ApplicationReportingService applicationReportingService;

    private final ApplicationRelevancyReportingService applicationRelevancyReportingService;

    private final ApplicationButtonService applicationButtonService;

    private final ApplicationRobotService applicationRobotService;

    /**
     * 应用类型
     *
     * @return ApplicationTypeEnum
     */
    @Override
    public ApplicationTypeEnum type() {
        return ApplicationTypeEnum.FORM_APP;
    }

    /**
     * 新增应用
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    public void addApplication(ApplicationInfo applicationInfo) {
        // 初始化提交配置
        applicationApplySettingService.initApplySetting(applicationInfo);

        // 初始化提醒设置
        applicationNotifySettingService.initNotifySetting(applicationInfo);

        // 初始化表单
        applicationFormService.initForm(applicationInfo);
    }

    /**
     * 删除应用
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    public void deleteApplication(ApplicationInfo applicationInfo) {
        // 删除提交设置
        applicationApplySettingService.deleteApplySetting(applicationInfo);

        // 删除提醒设置
        applicationNotifySettingService.deleteNotifySetting(applicationInfo);

        // 删除报表
        applicationReportingService.deleteByApp(applicationInfo);

        // 删除关联报表
        applicationRelevancyReportingService.deleteByApp(applicationInfo);

        // 删除应用表单
        applicationFormService.deleteForm(applicationInfo);
    }

    /**
     * 复制应用
     *
     * @param copyApplicationDTO copyApplicationDTO
     */
    @Override
    public void copyApplication(CopyApplicationDTO copyApplicationDTO) {
        // 复制提交设置
        applicationApplySettingService.copyApplySetting(new CopyAppApplySettingReqDTO(copyApplicationDTO));

        // 复制提醒设置
        applicationNotifySettingService.copyNotifySetting(new CopyAppNotifySettingReqDTO(copyApplicationDTO));

        // 复制表单
        Map<Integer, Map<Long, Long>> versionControlIdMap = applicationFormService.copyApplicationForm(new CopyAppFormReqDTO(copyApplicationDTO));
    }

    /**
     * 发布应用
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    public void publishApplication(ApplicationInfo applicationInfo) {
        // 发布应用提交配置
        applicationApplySettingService.publishApplySetting(applicationInfo);

        // 发布提交设置
        applicationNotifySettingService.publishApplicationNotifySetting(applicationInfo);

        // 发布应用表单
        CopyControlDTO copyControlDTO = applicationFormService.publishApplicationForm(applicationInfo);
        Map<Long, Long> controlIdMap = copyControlDTO.getControlIdMap();
        controlIdMap.putAll(StreamUtil.toMap(OrderSpecialControlEnum.getSpecialControlIds()));

        Map<Long, Long> optionIdMap = StreamUtil.concatMapValues(copyControlDTO.getControlOptionIdMap());

        // 发布自定义按钮
        applicationButtonService.publishButton(applicationInfo, controlIdMap);

        // 发布应用机器人
        applicationRobotService.publishRobot(applicationInfo, controlIdMap, optionIdMap);
    }

    /**
     * 还原应用
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    public void restoreApplication(ApplicationInfo applicationInfo) {
        // 还原应用提交配置
        applicationApplySettingService.restoreApplySetting(applicationInfo);

        // 还原应用提交设置
        applicationNotifySettingService.restoreApplicationNotifySetting(applicationInfo);

        // 还原应用表单
        CopyControlDTO copyControlDTO = applicationFormService.restoreApplicationForm(applicationInfo);
        Map<Long, Long> controlIdMap = copyControlDTO.getControlIdMap();
        controlIdMap.putAll(StreamUtil.toMap(OrderSpecialControlEnum.getSpecialControlIds()));

        Map<Long, Long> optionIdMap = StreamUtil.concatMapValues(copyControlDTO.getControlOptionIdMap());

        // 还原自定义按钮
        applicationButtonService.restoreButton(applicationInfo, controlIdMap);

        // 还原应用机器人
        applicationRobotService.restoreRobot(applicationInfo, controlIdMap, optionIdMap);
    }
}
